import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            in.nextLine();
            String str1 = in.next();
            String str2 = in.nextLine().trim();
            String[] strs = new String[n];
            for(int i = 0;i < n ;i ++){
                strs[i] = in.nextLine().trim();
            }
            int x = -1;
            int y = -1;
            int s = n;
            for(int i = 0;i < n;i ++){
                if(strs[i] != null && strs[i].equals(str1)){
                    x = i;
                }
                if(strs[i] != null && strs[i].equals(str2)){
                    y = i;
                }
                if(x != -1 && y != -1){
                    s = Math.min(Math.abs(y - x),s);

                }
            }
            if(x == -1 || y == -1){
                System.out.print(-1);
            }else{
                System.out.print(s);
            }
        }
    }
}